package com.example.demoamqp.send;

import cn.hutool.core.date.DateUtil;
import com.example.demoamqp.conatants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @className: Delay2Sender
 * @projectName: demo-one
 * @auth: rosszhang
 * @date: 2023/12/29 10:13
 */
@Slf4j
@Component
public class Delay2Sender {

    private AmqpTemplate rabbitAmqpTemplate;

    @Autowired
    public void setRabbitAmqpTemplate(AmqpTemplate rabbitAmqpTemplate) {
        this.rabbitAmqpTemplate = rabbitAmqpTemplate;
    }


    /**
     *
     * @param msg 消息
     * @param delayTime 延迟时间 单位（毫秒）
     */
    public void sendDelay2(String msg,int delayTime){
        rabbitAmqpTemplate.convertAndSend(
                Constants.MQ_NORMAL_EXCHANGE,
                Constants.MQ_NORMAL_ROUTING_KEY,
                msg,
                process -> {
                    process.getMessageProperties().setExpiration(String.valueOf(delayTime));
                    return process;
                }
        );
        log.debug("[生产者：]发送消息:{},时间{},延迟{}秒", msg, DateUtil.date(), delayTime / 1000);
    }
}
